<template>
  <div class="login-container">
    <el-card class="login-box animated">
      <h2 class="title">武汉市房产分析网</h2>

      <el-tabs v-model="activeTab" class="tabs-container" stretch>
        <el-tab-pane label="用户登录" name="login" class="tab-pane">
          <transition name="fade" mode="out-in">
            <div :key="activeTab">
              <el-form :model="loginForm" @submit.prevent="handleLogin" ref="loginFormRef" label-position="top">
                <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]">
                  <el-input v-model="loginForm.username" placeholder="请输入用户名" clearable />
                </el-form-item>

                <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
                  <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password clearable />
                </el-form-item>

                <el-form-item class="button-group">
                  <el-button type="primary" native-type="submit" :loading="loading" style="width: 100%;">登录</el-button>
                </el-form-item>
              </el-form>
            </div>
          </transition>
        </el-tab-pane>

        <el-tab-pane label="新用户注册" name="register" class="tab-pane">
          <transition name="fade" mode="out-in">
             <div :key="activeTab">
              <el-form :model="registerForm" @submit.prevent="handleRegister" ref="registerFormRef" label-position="top">
                <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]">
                  <el-input v-model="registerForm.username" placeholder="设置您的用户名" clearable />
                </el-form-item>

                <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
                  <el-input v-model="registerForm.password" type="password" placeholder="设置您的密码" show-password clearable />
                </el-form-item>

                <el-form-item class="button-group">
                  <el-button type="success" native-type="submit" :loading="loading" style="width: 100%;">注册</el-button>
                </el-form-item>
              </el-form>
            </div>
          </transition>
        </el-tab-pane>
      </el-tabs>

      <div class="guest-access">
        <el-divider content-position="center"></el-divider>
        <el-button type="info" plain @click="handleGuestAccess" style="width: 100%;" :loading="loading">作为游客访问地图</el-button>
      </div>

    </el-card>
  </div>
</template>

<script setup>
// <script>部分与之前完全相同，所有功能保持不变
import { ref } from 'vue';
import { useAuthStore } from '@/stores/auth';
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';

const authStore = useAuthStore();
const router = useRouter();
const route = useRoute();
const activeTab = ref('login');
const loading = ref(false);

const loginFormRef = ref(null);
const registerFormRef = ref(null);

const loginForm = ref({
  username: '',
  password: ''
});

const registerForm = ref({
  username: '',
  password: ''
});

const handleLogin = async () => {
  if (!loginFormRef.value) return;
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        await authStore.login(loginForm.value);
        ElMessage.success('登录成功');
        const redirectPath = route.query.redirect || (authStore.isAdmin ? '/admin' : '/map');
        router.push(redirectPath);
      } catch (error) {
        console.error('Login component caught error:', error.message);
      } finally {
        loading.value = false;
      }
    } else {
      ElMessage.error('请检查表单输入项');
      return false;
    }
  });
};

const handleRegister = async () => {
  if (!registerFormRef.value) return;
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        await authStore.register(registerForm.value);
        ElMessage.success('注册成功！您的 API Key 已可在登录后的个人中心查看。请现在登录。');
        activeTab.value = 'login';
        registerForm.value.username = '';
        registerForm.value.password = '';
      } catch (error) {
        console.error('Register component caught error:', error.message);
      } finally {
        loading.value = false;
      }
    } else {
      ElMessage.error('请检查表单输入项');
      return false;
    }
  });
};

const handleGuestAccess = () => {
  router.push('/map');
};
</script>

<style scoped>
/* --- Keyframes for Animations (无变化) --- */
@keyframes slideUpFadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* --- 主要容器与背景图片设置 --- */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 0;
  overflow: hidden;

  /* --- 背景图片设置区域 --- */
  /* 使用您自己的图片URL替换这里的路径 */
  background-image: url('/login_background.jpg');
  background-size: cover; /* 确保图片覆盖整个容器 */
  background-position: center; /* 图片居中显示 */
  background-repeat: no-repeat; /* 防止图片平铺重复 */
}

/* --- 登录卡片 (半透明毛玻璃效果) --- */
.login-box {
  width: 100%;
  max-width: 420px;
  padding: 40px;
  border-radius: 12px;

  /* --- 半透明效果的关键代码 --- */
  /* 使用 rgba 设置半透明背景色，0.7表示70%的不透明度 */
  background: rgba(255, 255, 255, 0.7);
  /* backdrop-filter是实现毛玻璃效果的核心，让背景模糊 */
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px); /* 兼容Safari浏览器 */
  border: 1.5px solid rgba(255, 255, 255, 0.25); /* 边框也设为半透明 */

  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.login-box:hover {
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
    transform: translateY(-5px); /* 添加悬浮时的轻微上移效果 */
}

.login-box.animated {
    animation: slideUpFadeIn 0.7s ease-out forwards;
}

/* --- 标题 (无变化) --- */
.title {
  text-align: center;
  font-size: 26px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 30px;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.05);
}

/* --- 交互元素 (无变化) --- */
:deep(.el-input__wrapper) {
  transition: all 0.3s ease;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  background-color: rgba(255, 255, 255, 0.8); /* 输入框也轻微透明以融入整体风格 */
}
:deep(.el-input__wrapper.is-focus) {
  background-color: #fff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.3);
}
.el-button {
  transition: all 0.3s ease;
}
.el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

/* --- 其他样式 (无变化) --- */
.tabs-container { margin-bottom: 20px; }
.tab-pane { padding-top: 10px; }
.button-group { margin-top: 20px; }
.guest-access { margin-top: 20px; text-align: center; }
.guest-access .el-divider { margin: 25px 0; font-size: 14px; color: #303133; font-weight: 500;}
.fade-enter-active, .fade-leave-active { transition: opacity 0.3s ease; }
.fade-enter-from, .fade-leave-to { opacity: 0; }

</style>